package faceb.server.dao.impl;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import faceb.server.dao.CommentDao;
import faceb.server.model.Comment;
import faceb.server.util.MyBatisUtil;
import faceb.server.util.ServerFinal;

@Repository("commentDao")
public class CommentDaoImpl implements CommentDao {

	private SqlSessionFactory factory = MyBatisUtil.getsqlSessionFactory();

	public List<Map<String,Object>> getCommentByNewsId(String newsid) {
		SqlSession session = factory.openSession();
		List<Map<String,Object>> comments = session.selectList(
				"CommentMapper.getCommentByNewsId", newsid);
		for (Map<String,Object> comment:comments ) {
			comment.put("image", ServerFinal.SERVER_URL+"userlogo/users/"+comment.get("u_id"));
			comment.put("c_time", new Date((Long)comment.get("c_time")).toLocaleString());
		}
		session.close();
		return comments;
	}

	public int setComment(Comment comment) {
		SqlSession session = factory.openSession();
		int result = session
				.insert("CommentMapper.setComment", comment);
		session.commit();
		session.close();
		return result;
	}


	
	public static void main(String[] args) {
		List<Map<String,Object>> comments = new CommentDaoImpl().getCommentByNewsId("335b23a096b74c19bc6c78e46520d045");
		for (Map<String,Object> comment :comments) {
			System.out.println(comment);
		}
	}
}
